//
//  ViewController.m
//  三原色
//
//  Created by student on 16/2/19.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (nonatomic,weak) UIView *showView;
@property (nonatomic,weak) UISlider *slider1;
@property (nonatomic,weak) UISlider *slider2;
@property (nonatomic,weak) UISlider *slider3;
@property (nonatomic,weak) UITextView *textView1;
@property (nonatomic,weak) UITextView *textView2;
@property (nonatomic,weak) UITextView *textView3;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self setUpUI];
}


-(void)setUpUI{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 315, 150)];
    [self.view addSubview:view];
    self.showView = view;
    [self setUpLabelWithName:@"Red" WithFrame:CGRectMake(30, 200, 30, 30)];
    [self setUpLabelWithName:@"Green" WithFrame:CGRectMake(30, 250, 30, 30)];
    [self setUpLabelWithName:@"Blue" WithFrame:CGRectMake(30, 300, 30, 30)];
    self.slider1 = [self setUpSliderWithFrame:CGRectMake(80, 200, 240, 30)];
    self.slider2 = [self setUpSliderWithFrame:CGRectMake(80, 250, 240, 30)];
    self.slider3 = [self setUpSliderWithFrame:CGRectMake(80, 300, 240, 30)];
    self.textView1 = [self setUpTextViewWithFrame:CGRectMake(340, 200, 50, 30)];
    self.textView1.text = @"123";
    self.textView2 = [self setUpTextViewWithFrame:CGRectMake(340, 250, 50, 30)];
    self.textView2.text = @"123";
    self.textView3 = [self setUpTextViewWithFrame:CGRectMake(340, 300, 50, 30)];
    self.textView3.text = @"123";
    [self changeVaule];
}
//设置label
-(void)setUpLabelWithName:(NSString *)name
                WithFrame:(CGRect)frame{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    [label setText:name];
    [label setFont:[UIFont systemFontOfSize:12]];
    [self.view addSubview:label];
    
}
//设置slider
-(UISlider *)setUpSliderWithFrame:(CGRect)frame{
    UISlider *slider = [[UISlider alloc] initWithFrame:frame];
    slider.minimumValue = 0;
    slider.maximumValue = 255;
    slider.value = 123;
    [self.view addSubview:slider];
    [slider addTarget:self action:@selector(changeVaule) forControlEvents:UIControlEventValueChanged];
    return slider;
}
//设置textView
-(UITextView *)setUpTextViewWithFrame:(CGRect)frame{
    UITextView *textView = [[UITextView alloc] initWithFrame:frame];
    [textView setTextColor:[UIColor blackColor]];
    textView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:textView];
    return textView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//改变颜色
-(void)changeVaule{
    [self.showView setBackgroundColor:[UIColor colorWithRed:self.slider1.value/255 green:self.slider2.value/255 blue:self.slider3.value/255 alpha:1]];
    [self textViewVaule:self.textView1 SliderVaule:self.slider1];
    [self textViewVaule:self.textView2 SliderVaule:self.slider2];
    [self textViewVaule:self.textView3 SliderVaule:self.slider3];
}
//textView中的值跟随slider中的值改变而改变
-(void)textViewVaule:(UITextView *)textView
         SliderVaule:(UISlider *) sender{
    int s = (int)sender.value;
    textView.text = [NSString stringWithFormat:@"%i",s];
}
//slider中的值跟随textView中的值改变而改变
-(void)sliderVaule:(UISlider *)slider
     textViewVaule:(UITextView *)textView{
    int s = [textView.text intValue];
    if (s > 255) {
        s = 255;
    }
    else if (s < 0){
        s = 0;
    }
    slider.value = s;
}
- (IBAction)keyBoard:(UIControl *)sender {
    [self.view endEditing:YES];
    [self sliderVaule:self.slider1 textViewVaule:self.textView1];
    [self sliderVaule:self.slider2 textViewVaule:self.textView2];
    [self sliderVaule:self.slider3 textViewVaule:self.textView3];
    [self changeVaule];
}

@end
